package org.langcom.change;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RefreshManager implements IRefreshable {

	private String name;
	private List<IRefreshable> refreshList = Collections.synchronizedList(new ArrayList<IRefreshable>());
	private Long lastRefresh;

	public RefreshManager(String name) {
		this.name = name;
	}

	public synchronized void addRefreshable(IRefreshable refreshable) {
		refreshList.add(refreshable);
	}

	public synchronized void removeRefreshable(IRefreshable refreshable) {
		refreshList.remove(refreshable);
	}

	public synchronized void refresh(IRefreshable source) {
		if ((lastRefresh != null) && ((System.currentTimeMillis() - lastRefresh) < 300)) {
			return;
		}
		lastRefresh = System.currentTimeMillis();
		for (IRefreshable refreshable : refreshList) {
			if (source != null) {
				if (!refreshable.equals(source)) {
					refreshable.refresh(source);
				}
			} else {
				refreshable.refresh(null);
			}
		}
		if (source != null) {
			source.refreshInt();
		}
	}

	public String getName() {
		return name;
	}

	@Override
	public void refreshInt() {
	}

	@Override
	public int refreshHashCode() {
		return 0;
	}
}
